home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Offline Browsing / HTTrack.exe / data1.cab / Sources / src / htscatchurl.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-04-28  |  7.9 KB  |  293 lines

  1. /* ------------------------------------------------------------ */
  2. /*
  3. HTTrack Website Copier, Offline Browser for Windows and Unix
  4. Copyright (C) Xavier Roche and other contributors
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19.  
  20.  
  21. Important notes:
  22.  
  23. - We hereby ask people using this source NOT to use it in purpose of grabbing
  24. emails addresses, or collecting any other private information on persons.
  25. This would disgrace our work, and spoil the many hours we spent on it.
  26.  
  27.  
  28. Please visit our Website: http://www.httrack.com
  29. */
  30.  
  31.  
  32. /* ------------------------------------------------------------ */
  33. /* File: URL catch .h                                           */
  34. /* Author: Xavier Roche                                         */
  35. /* ------------------------------------------------------------ */
  36.  
  37. // Fichier intercepteur d'URL .c
  38.  
  39. /* specific definitions */
  40. /* specific definitions */
  41. #include "htsbase.h"
  42. #include "htsnet.h"
  43. #include "htslib.h"
  44. #include <stdio.h>
  45. #include <stdlib.h>
  46. #include <string.h>
  47. #include <time.h>
  48. #include <fcntl.h>
  49. #if HTS_WIN
  50. #else
  51. #include <arpa/inet.h>
  52. #endif
  53. /* END specific definitions */
  54.  
  55. /* dΘfinitions globales */
  56. #include "htsglobal.h"
  57.  
  58. /* htslib */
  59. /*#include "htslib.h"*/
  60.  
  61. /* catch url */
  62. #include "htscatchurl.h"
  63.  
  64.  
  65. // URL Link catcher
  66.  
  67. // 0- Init the URL catcher with standard port
  68.  
  69. // catch_url_init(&port,&return_host);
  70. T_SOC catch_url_init_std(int* port_prox,char* adr_prox) {
  71.   T_SOC soc;
  72.   int try_to_listen_to[]={8080,3128,80,81,82,8081,3129,31337,0,-1};
  73.   int i=0;
  74.   do {
  75.     soc=catch_url_init(&try_to_listen_to[i],adr_prox);
  76.     *port_prox=try_to_listen_to[i];
  77.     i++;
  78.   } while( (soc == INVALID_SOCKET) && (try_to_listen_to[i]>=0));
  79.   return soc;
  80. }
  81.  
  82.  
  83. // 1- Init the URL catcher
  84.  
  85. // catch_url_init(&port,&return_host);
  86. T_SOC catch_url_init(int* port,char* adr) {
  87.   T_SOC soc = INVALID_SOCKET;
  88.   char h_loc[256+2];
  89.  
  90.   /*
  91. #ifdef _WIN32
  92.   {
  93.     WORD   wVersionRequested;
  94.     WSADATA wsadata;
  95.     int stat;
  96.     wVersionRequested = 0x0101;
  97.     stat = WSAStartup( wVersionRequested, &wsadata );
  98.     if (stat != 0) {
  99.       return INVALID_SOCKET;
  100.     } else if (LOBYTE(wsadata.wVersion) != 1  && HIBYTE(wsadata.wVersion) != 1) {
  101.       WSACleanup();
  102.       return INVALID_SOCKET;
  103.     }
  104.   }
  105. #endif
  106.   */
  107.  
  108.   if (gethostname(h_loc,256)==0) {    // host name
  109.     t_hostent* hp_loc;    
  110.     if ( (hp_loc=gethostbyname(h_loc)) ) {  // notre host      
  111.       if ( (soc=socket(AF_INET,SOCK_STREAM,0)) != INVALID_SOCKET) {
  112.         struct sockaddr_in server;
  113.         // effacer structure
  114.         bzero((char *)&server, sizeof(server));
  115.         server.sin_family = AF_INET;
  116.         server.sin_port = htons((unsigned short int) *port);    // demander port qque
  117.         // copie adresse locale
  118.         bcopy(hp_loc->h_addr, (char *)&server.sin_addr, hp_loc->h_length);
  119.         // // NAAN server.sin_addr.s_addr=htonl(INADDR_ANY);
  120.         if ( bind(soc,(struct sockaddr*) &server,sizeof(struct sockaddr)) == 0 ) {
  121.           struct sockaddr_in server2;
  122.           int len;
  123.           len=sizeof(server2);
  124.           // effacer structure
  125.           bzero((char *)&server2, sizeof(server2));
  126.           if (getsockname(soc,(struct sockaddr*) &server2,&len) == 0) {
  127.             *port=ntohs(server.sin_port);  // rΘcupΘrer port
  128.             if (listen(soc,10)>=0) {    // au pif le 10
  129.               char* dot = (char*) inet_ntoa(server2.sin_addr);
  130.               strcpy(adr,dot);
  131.             } else {
  132. #if _WIN32
  133.               closesocket(soc);
  134. #else
  135.               close(soc);
  136. #endif
  137.               soc=INVALID_SOCKET;
  138.             }
  139.             
  140.             
  141.           } else {
  142. #if _WIN32
  143.             closesocket(soc);
  144. #else
  145.             close(soc);
  146. #endif
  147.             soc=INVALID_SOCKET;
  148.           }
  149.           
  150.           
  151.         } else {
  152. #if _WIN32
  153.           closesocket(soc);
  154. #else
  155.           close(soc);
  156. #endif
  157.           soc=INVALID_SOCKET;
  158.         }
  159.       }
  160.     }
  161.   }
  162.   return soc;
  163. }
  164.  
  165. // 2 - Wait for URL
  166.  
  167. // catch_url
  168. // returns 0 if error
  169. // url: buffer where URL must be stored - or ip:port in case of failure
  170. // data: 32Kb
  171. int catch_url(T_SOC soc,char* url,char* method,char* data) {
  172.   int retour=0;
  173.  
  174.   // connexion (accept)
  175.   if (soc != INVALID_SOCKET) {
  176.     T_SOC soc2;
  177.     struct sockaddr dummyaddr;
  178.     int dummylen = sizeof(struct sockaddr);  
  179.     while ( (soc2=accept(soc,&dummyaddr,&dummylen)) == INVALID_SOCKET);
  180.   /*
  181. #ifdef _WIN32
  182.     closesocket(soc);
  183. #else
  184.     close(soc);
  185. #endif
  186.   */
  187.     soc = soc2;
  188.     /* INFOS */
  189.     {
  190.       struct sockaddr_in server2;
  191.       int len;
  192.       len=sizeof(server2);
  193.       // effacer structure
  194.       bzero((char *)&server2, sizeof(server2));
  195.       if (getpeername(soc,(struct sockaddr*) &server2,&len) == 0) {
  196.         char* dot = (char*) inet_ntoa(server2.sin_addr);
  197.         sprintf(url,"%s:%d",dot,htons(server2.sin_port));  
  198.       }
  199.     }
  200.     /* INFOS */
  201.  
  202.     // rΘception
  203.     if (soc != INVALID_SOCKET) {
  204.       char line[1000];
  205.       char protocol[256];
  206.       line[0]=protocol[0]='\0';
  207.       //
  208.       socinput(soc,line,1000);
  209.       if (strnotempty(line)) {
  210.         if (sscanf(line,"%s %s %s",method,url,protocol) == 3) {
  211.           char url_adr[HTS_URLMAXSIZE*2];
  212.           char url_fil[HTS_URLMAXSIZE*2];
  213.           // mΘthode en majuscule
  214.           int i,r=0;
  215.           url_adr[0]=url_fil[0]='\0';
  216.           //
  217.           for(i=0;i<(int) strlen(method);i++) {
  218.             if ((method[i]>='a') && (method[i]<='z'))
  219.               method[i]-=('a'-'A');
  220.           }
  221.           // adresse du lien
  222.           if (ident_url(url,url_adr,url_fil)>=0) {
  223.             // Traitement des en-tΩtes
  224.             char loc[HTS_URLMAXSIZE*2];
  225.             htsblk blkretour;
  226.             bzero((char *)&blkretour, sizeof(htsblk));    // effacer
  227.             blkretour.location=loc;    // si non nul, contiendra l'adresse vΘritable en cas de moved xx
  228.             // Lire en tΩtes restants
  229.             sprintf(data,"%s %s %s\r\n",method,url_fil,protocol);
  230.             while(strnotempty(line)) {
  231.               socinput(soc,line,1000);
  232.               treathead(NULL,NULL,NULL,&blkretour,line);  // traiter
  233.               strcat(data,line);
  234.               strcat(data,"\r\n");
  235.             }
  236.             // CR/LF final de l'en tΩte inutile car dΘja placΘ via la ligne vide juste au dessus
  237.             //strcat(data,"\r\n");
  238.             if (blkretour.totalsize>0) {
  239.               int len=(int)min(blkretour.totalsize,32000);
  240.               int pos=strlen(data);
  241.               // Copier le reste (post Θventuel)
  242.               while((len>0) && ((r=recv(soc,(char*) data+pos,len,0))>0) ) {
  243.                 pos+=r;
  244.                 len-=r;
  245.                 data[pos]='\0';       // terminer par NULL
  246.               }
  247.             }
  248.             // Envoyer page
  249.             sprintf(line,CATCH_RESPONSE);
  250.             send(soc,line,strlen(line),0);
  251.             // OK!
  252.             retour=1;
  253.           }
  254.         }
  255.       }  // sinon erreur
  256.     }
  257.   }
  258.   if (soc != INVALID_SOCKET) {
  259. #ifdef _WIN32
  260.     closesocket(soc);
  261.     /*
  262.     WSACleanup();
  263.     */
  264. #else
  265.     close(soc);
  266. #endif
  267.   }
  268.   return retour;
  269. }
  270.  
  271.  
  272.  
  273. // Lecture de ligne sur socket
  274. void socinput(T_SOC soc,char* s,int max) {
  275.   int c;
  276.   int j=0;
  277.   do {
  278.     unsigned char b;
  279.     if (recv(soc,(char*) &b,1,0)==1) {
  280.       c=b;
  281.       switch(c) {
  282.         case 13: break;  // sauter CR
  283.         case 10: c=-1; break;
  284.         case 9: case 12: break;  // sauter ces caractΦres
  285.         default: s[j++]=(char) c; break;
  286.       }
  287.     } else
  288.       c=EOF;
  289.   }  while((c!=-1) && (c!=EOF) && (j<(max-1)));
  290.   s[j++]='\0';
  291. }
  292.  
  293.